<template>
	<view class="container">
		<view class="input_box ">
			<view class="title hh">收货人</view>
			<input type="text" v-model="name" placeholder="请输入收货人姓名">
		</view>
		<view class="input_box">
			<view class="title">手机号码</view>
			<input type="number" v-model="mobile" maxlength="11" placeholder="请输入收货人手机号码">
		</view>
		<!-- <view class="input_box">
			<view class="title">所在地区</view>
			<input type="number" v-model="city" placeholder="点击选择地址" disabled="" @click="show = true">
			<view class="icon_box" @click="lookLoaction">
				<u-icon name="map-fill" color="#ea3d37" size="50"></u-icon>
			</view>
		</view> -->
		<view class="input_box">
			<view class="title">所在地区</view>
			<input type="text" v-model="dizhi" placeholder="请选择收货人所在地区">
			<view class="icon_box" @click="lookLoaction">
				<u-icon name="map-fill" color="#ea3d37" size="50"></u-icon>
			</view>
		</view>
		<view class="input_box">
			<view class="title">详细地址</view>
			<input type="text" v-model="address" placeholder="请填写详细地址如：街道、门牌号等">
		</view>
		<view class="bottom">
			<view class="title">设置默认地址</view>
			<view class="btn_S">
				<view class="text">提醒：每次下单时会使用该地址,实际下单前请确认好地址在下单哦!</view>
				<u-switch v-model="checked" active-color="#EA3D37"></u-switch>
			</view>
		</view>
		<view class="sebtns" @click="addbank">保存</view>
		<u-picker v-model="show" mode="region" @confirm="confirm"></u-picker>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				name: '',
				address: '',
				mobile: '',
				city: '',
				dizhi: '',
				checked: false,
				id: null,
				show: false,
				addressObj: {
					province: '',
					province_id: '',
					city: '',
					city_id: '',
					district: '',
					district_id: ''
				}
			}
		},
		onLoad() {
			// this.getArea()
		},
		methods: {
			lookLoaction() {
				uni.chooseLocation({
					success: res => {
						console.log(res);
						console.log('位置名称：' + res.name);
						console.log('详细地址：' + res.address);
						console.log('纬度：' + res.latitude);
						console.log('经度：' + res.longitude);
						this.dizhi = res.address
					}
				});
			},
			getArea() {
				this.$u.api.getArea().then(res => {

				})
			},
			mapClick() {
				uni.chooseLocation({
					success: res => {
						console.log(res)
						this.city = res.address
					}
				});
			},
			confirm(e) {
				this.city = e.province.label + '-' + e.city.label + '-' + e.area.label
				this.addressObj.province = e.province.label
				this.addressObj.province_id = e.province.value
				this.addressObj.city = e.city.label
				this.addressObj.city_id = e.city.value
				this.addressObj.district = e.area.label
				this.addressObj.district_id = e.area.value
			},
			addbank() {
				let isDefault = ''
				if (this.checked) {
					isDefault = '1'
				} else {
					isDefault = '0'
				}
				if (this.name == '') {
					this.$u.toast('请输入收货人姓名')
					return
				} else if (this.mobile == '') {
					this.$u.toast('请输入收货人手机号码')
					return
				} else if (this.dizhi == '') {
					this.$u.toast('收货人地区不能为空')
					return
				} else if (this.address == '') {
					this.$u.toast('请填写详细地址')
					return
				} else {
					let model = {
						// id: this.id,
						real_name: this.name,
						phone: this.mobile,
						// detail: this.address,
						post_code: '641500',
						id: 0,
						is_default: isDefault,
						detail: this.address
						// province_code: this.province,
						// city_code: this.citys,
						// area_code: this.area,
					}
					this.$allApi.addAddress(model).then(res => {
						this.$u.toast('添加地址成功')
						setTimeout(() => {
							this.$backTo(1)
						}, 2000)
					})
				}
			}
		}
	}
</script>

<style scoped lang="scss">
	.icon_box {
		width: 100rpx;
		display: flex;
		align-items: center;
		justify-content: center;
	}

	.bottom {
		margin-top: 20rpx;
		background: #fff;
		padding: 20rpx;
		color: #666;
		line-height: 1.8;

		.title {
			font-size: 30rpx;
		}

		.btn_S {
			display: flex;
			align-items: center;
			justify-content: space-between;

			.text {
				font-size: 24rpx;
				flex: 1;
				padding-right: 20rpx;
			}
		}
	}

	.container {
		min-height: 100vh;
		background: #f6f6f6;
	}

	.input_box {
		display: flex;
		align-items: center;
		height: 110rpx;
		border-bottom: 1px solid #eee;
		padding: 20rpx;
		background: #fff;

		.title {}

		.hh {
			letter-spacing: 0.5em;
			/*如果需要y个字两端对齐，则为(x-y)/(y-1),这里是（4-3）/(3-1)=0.5em */
			margin-right: -0.5em;
			/*同上*/
		}

		input {
			flex: 1;
			padding-left: 30rpx;
		}
	}

	.sebtns {
		position: fixed;
		bottom: 0;
		left: 0;
		width: 100vw;
		background: #EA3D37;
		color: #fff;
		font-size: 32rpx;
		display: flex;
		align-items: center;
		justify-content: center;
		border-radius: 35rpx;
		height: 70rpx;

		bottom: calc(20rpx + constant(safe-area-inset-bottom));
		bottom: calc(20rpx + env(safe-area-inset-bottom));
	}
</style>